﻿using System;
using System.Linq;
using System.Web.Mvc;
using KPIS.GERP.Models.ICM;

namespace KPIS.GERP.WEB.GIMS.Controllers
{
    // [AccessDeniedAuthorize]
    public class DeliveryNoteItemController : Controller
    {
       

        public ActionResult Index(int? PoPenId,int DnId)
        {
            DeliveryNoteItem oModel = new DeliveryNoteItem();
            var service =new DeliveryNoteItemService.DeliveryNoteItemServiceClient();
            var PIOrder = service.SelectDeliveryNoteItem(new Models.ICM.DeliveryNoteItem { DnId = DnId }, new[] { "PoInstallItem", "PoInstallItem.BidderQuotation.PurchaseRequestItem.MasResourceItem", "PoInstallItem.BidderQuotation.PurchaseRequestItem.MasResourceItem.MasUnit" });
            
            var service1 = new DeliveryNoteService.DeliveryNoteServiceClient();
            var modelDn = service1.SelectDeliveryNote(new Models.ICM.DeliveryNote { DnId = DnId }, null).FirstOrDefault();

            string RecordStatus = modelDn.RecordStatus;
            ViewBag.bEditable = RecordStatus != Models.SYS.RecordStatus.ApprovedStatus;

            return View(PIOrder);
        }
        public ActionResult Create(int? PoInstallId)
        {
            DeliveryNoteItem oModel = new DeliveryNoteItem();
            var service = new DeliveryNoteService.DeliveryNoteServiceClient();
            var PIOrder = service.SelectPurchaseOrderInsatall(new Models.ICM.PurchaseOrderInstallment { }, new[] { "PurchaseOrder.MasPurchaseType", "PurchaseOrder.Bidder.Bid" });
            
            oModel.PoInstallItemId = PoInstallId;
            var oData = PIOrder.Where(t =>  t.PoInstallId == PoInstallId).SingleOrDefault();

            if (oData != null)
            {
                oModel.Descr = oData.Remarks;
              
            }

            oModel.RecordStatus = "U";
            return View(oModel);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(DeliveryNoteItem model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var service = new DeliveryNoteItemService.DeliveryNoteItemServiceClient();
                    var id = service.Insert(model, Class.GimsMembership.CurrentEmpId);
                    return RedirectToAction("Index", new { id });
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return View(model);
        }


        public ActionResult Edit(int id)
        {
            var service = new DeliveryNoteItemService.DeliveryNoteItemServiceClient();
            var item = service.SelectDeliveryNoteItem(new Models.ICM.DeliveryNoteItem { DnItemId = id, RecordStatus = "!D" }, null).FirstOrDefault();

            if (item == null)
            {
                return HttpNotFound();
            }
            return View(item);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, DeliveryNoteItem model)
        {
            try
            {
                if (ModelState.IsValid)
                {

                    var service = new DeliveryNoteItemService.DeliveryNoteItemServiceClient();
                    service.Update(model, Class.GimsMembership.CurrentEmpId);

                    return RedirectToAction("Index");
                }
                return Edit(model.DnItemId);
            }
            catch
            {
                return Edit(model.DnItemId);
            }
        }

        public void UpdateHaveItem(int DnItemId, string HaveItem)
        {
            try
            {
                var service = new DeliveryNoteItemService.DeliveryNoteItemServiceClient();
                var model = service.SelectDeliveryNoteItem(new DeliveryNoteItem() { DnItemId = DnItemId }, null).FirstOrDefault();
                model.HaveItem = HaveItem.ToLower() == "false";
                service.Update(model, Class.GimsMembership.CurrentEmpId);
            }
            catch
            {
                
            }
        }
    }
}
